package com.thb.pay.payment.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.thb.pay.api.payment.PayPassageServiceApi;
import com.thb.pay.api.payment.vo.req.PayPassagePageReq;
import com.thb.pay.api.payment.vo.req.PayPassageReq;
import com.thb.pay.api.payment.vo.resp.PayPassageVo;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.entity.SuperEntity;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.payment.entity.PayPassage;
import com.thb.pay.payment.service.PayPassageService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 支付-通道表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@RestController
public class PayPassageController implements PayPassageServiceApi {

    @Autowired
    private PayPassageService payPassageService;

    @Override
    public PageResult<PayPassageVo> findPage(@RequestBody PayPassagePageReq pageReq) {
        IPage<PayPassage> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<PayPassage> pageResult = payPassageService.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, PayPassageVo.class);
    }

    @Override
    public void add(@RequestBody PayPassageReq payPassageReq) {
        PayPassage payPassage = new PayPassage();
        BeanUtil.copyProperties(payPassageReq, payPassage);
        payPassageService.insert(payPassage);
    }

    @Override
    public void update(@RequestBody PayPassageReq payPassageReq) {
        PayPassage payPassage = payPassageService.selectById(payPassageReq.getId());
        if (payPassage == null) {
            throw RRException.build("passage.not.exist");
        }
        PayPassage targetPayPassage = new PayPassage();
        BeanUtil.copyProperties(payPassageReq, targetPayPassage);
        payPassageService.updateById(targetPayPassage);
    }

    @Override
    public PayPassageVo getById(@RequestParam("id") Long id) {
        PayPassage payPassage = payPassageService.selectById(id);
        if (payPassage != null) {
            PayPassageVo resultData = new PayPassageVo();
            BeanUtil.copyProperties(payPassage, resultData);
            return resultData;
        }
        return null;
    }

    @Override
    public void deleteBatchIds(@RequestBody List<Long> ids) {
        LambdaQueryWrapper<PayPassage> wrapper = new LambdaQueryWrapper();
        wrapper.in(PayPassage::getId, ids);
        List<PayPassage> targetDbResultList = payPassageService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            targetDbResultList.forEach(payType -> {
                payType.setDelFlag(SuperEntity.DEFAULT_DELETE_FLAG);
            });
            payPassageService.updateBatchById(targetDbResultList);
        }
    }
}
